package com.tj.collection.controller.admin.response;


import com.fqgj.common.api.Page;
import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.CollectionUtils;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.tj.collection.db.domain.AdminAndRole;
import com.tj.collection.db.domain.Role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2016/11/22
 * Time: 下午1:25
 */
public class AdminListResponse implements ResponseData {
    private Page page;
    private List<Item> list = new ArrayList<>();

    public AdminListResponse(List<AdminAndRole> adminAndRoleList, List<ProductCategory> productCategoryList, Page page) {
        this.page = page;
        if (CollectionUtils.isNotEmpty(adminAndRoleList)) {
            for (int i = 0; i < adminAndRoleList.size(); i++) {
                AdminAndRole adminAndRole = adminAndRoleList.get(i);
                Item item = new Item()
                        .setName(adminAndRole.getAccount())
                        .setId(adminAndRole.getId())
                        .setFullName(adminAndRole.getName())
                        .setProductType(this.getProductName(productCategoryList, adminAndRole.getProductType()))
                        .setTelephone(adminAndRole.getPhone())
                        .setMobilephone(adminAndRole.getMobile())
                        .setStatus(adminAndRole.getAdminStatusEnum().ordinal())
                        .setRoles(this.getRoles(adminAndRole.getRoles()).get("roleName").toString())
                        .setRoleIds((List<Long>) this.getRoles(adminAndRole.getRoles()).get("roleIds"));

                list.add(item);
            }
        }

    }

    private String getProductName(List<ProductCategory> productCategoryList, String productType) {
        if (productType.equals("all")) {
            return "通用";
        }
        if (CollectionUtils.isNotEmpty(productCategoryList)) {
            for (ProductCategory productCategory : productCategoryList) {
                if (productCategory.getCode().equals(productType)) {
                    return productCategory.getName();
                }
            }
        }
        return null;
    }

    public Map<String, Object> getRoles(List<Role> roleList) {
        StringBuilder roleMsg = new StringBuilder();
        List<Long> roleIds = new ArrayList<>();
        for (Role role : roleList) {
            roleIds.add(role.getRoleId());
            roleMsg.append(role.getName());
            roleMsg.append(";");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("roleIds", roleIds);
        map.put("roleName", roleMsg);
        return map;
    }


    private class Item {
        private Long id;
        private String name;
        private String fullName;
        private Integer type;
        private String typeMsg;
        private String mobilephone;
        private Integer status;
        private String telephone;
        private String productType;
        private String roles;
        private List<Long> roleIds;


        public String getRoles() {
            return roles;
        }

        public Item setRoles(String roles) {
            this.roles = roles;
            return this;
        }

        public List<Long> getRoleIds() {
            return roleIds;
        }

        public Item setRoleIds(List<Long> roleIds) {
            this.roleIds = roleIds;
            return this;
        }

        public Integer getStatus() {
            return status;
        }

        public Item setStatus(Integer status) {
            this.status = status;
            return this;
        }

        public String getTelephone() {
            return telephone;
        }

        public Item setTelephone(String telephone) {
            this.telephone = telephone;
            return this;
        }

        public String getProductType() {
            return productType;
        }

        public Item setProductType(String productType) {
            this.productType = productType;
            return this;
        }

        public String getMobilephone() {
            return mobilephone;
        }

        public Item setMobilephone(String mobilephone) {
            this.mobilephone = mobilephone;
            return this;
        }

        public String getTypeMsg() {
            return typeMsg;
        }

        public Item setTypeMsg(String typeMsg) {
            this.typeMsg = typeMsg;
            return this;
        }

        public Integer getType() {

            return type;
        }

        public Item setType(Integer type) {
            this.type = type;
            return this;
        }

        public Long getId() {
            return id;
        }

        public Item setId(Long id) {
            this.id = id;
            return this;
        }

        public String getName() {
            return name;
        }

        public Item setName(String name) {
            this.name = name;
            return this;
        }

        public String getFullName() {
            return fullName;
        }

        public Item setFullName(String fullName) {
            this.fullName = fullName;
            return this;
        }
    }
}
